	import {
		defineStore
	} from 'pinia';

	export const useCityStore = defineStore('CityStore', {
		state: () => {
			return {
				code: null,
				name: '',
				formatted: null,
				weekday_1: null,
				month_day_1: null,
				formatted_1: null,
				weekday_2: null,
				month_day_2: null,
				formatted_2: null,
				dayDiff: null,
				stay: [],
				stay_id: [],
				yh: null,
				yh_id: null,
				localtion: {
					longitude: 0,
					latitude: 0
				}
				// latitude:0
			}
		},
		// 也可以这样定义
		// state: () => ({ count: 0 })
		actions: {
			async getLocaltion() {
				return new Promise((resolve, reject) => {
					uni.getLocation({
						type: 'wgs84',
						altitude: true,
						geocode: true,
						isHighAccuracy: true,
						success(res) {
							resolve(res)
						},
						fail(res) {
							reject({
								localtion: {
									longitude: 0,
									latitude: 0
								}
							})

						}
					})
				})
			},
			async init() {
				if (this.localtion.longitude == 0 && this.localtion.latitude == 0) {
					this.localtion = await this.getLocaltion().then(res => {
						console.log(res);
						return res
					}).catch(() => {
						return {}
					})
				}
				await this.getTodayDate()
				await this.getTomorrowDate()
				await this.TodayTomorrow()

			},

			// 获取今天的年月日
			getTodayDate() {
				console.log(111, this.weekday_1);
				console.log(222, this.month_day_1);
				console.log(333, this.formatted_1);
				if (this.weekday_1 && this.month_day_1 && this.formatted_1) return
				const today = new Date()
				const year = today.getFullYear()
				const month = String(today.getMonth() + 1).padStart(2, '0')
				const day = String(today.getDate()).padStart(2, '0')
				const weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
				const weekday = weekdays[today.getDay()]

				this.weekday_1 = weekday
				this.month_day_1 = month + '月' + day + '日'
				this.formatted_1 = `${year}-${month}-${day}`
			},

			// 获取明天的年月日
			getTomorrowDate() {
				if (this.weekday_2 && this.month_day_2 && this.formatted_2) return
				const tomorrow = new Date()
				tomorrow.setDate(tomorrow.getDate() + 1)
				const year = tomorrow.getFullYear()
				const month = String(tomorrow.getMonth() + 1).padStart(2, '0')
				const day = String(tomorrow.getDate()).padStart(2, '0')
				const weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
				const weekday = weekdays[tomorrow.getDay()]
				this.weekday_2 = weekday
				this.month_day_2 = month + '月' + day + '日'
				this.formatted_2 = `${year}-${month}-${day}`
			},

			TodayTomorrow() {
				const startDate = new Date(this.formatted_1)
				const endDate = new Date(this.formatted_2)
				// 计算两个日期之间的毫秒差
				const timeDiff = endDate.getTime() - startDate.getTime()
				// 将毫秒转换为天数
				this.dayDiff = timeDiff / (1000 * 60 * 60 * 24)
				this.formatted = [this.formatted_1, this.formatted_2]

			},
			// 获取的年月日
			dayDate(value) {
				// console.log(value);
				const today = new Date(value)
				const year = today.getFullYear()
				const month = String(today.getMonth() + 1).padStart(2, '0')
				const day = String(today.getDate()).padStart(2, '0')
				const weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
				const weekday = weekdays[today.getDay()]
				return {
					weekday: weekday,
					month_day: month + '月' + day + '日',
					formatted: `${year}-${month}-${day}`
				}
			}

		},
	});